
/**
* Control event object, crossbroser wrapper for the dom event object and base for the MouseEvent and KeyEvent objects
* @extends Kwik.Object
* @requires Kwik.Object
* @constructor
* @created 11:24 AM 7/30/2009
* @modified 11:24 AM 7/30/2009
*/
Kwik.Ui.Controls.Event = function(config)
{
	/** @ignore */
	Kwik.Utility.extend(this, "Kwik.Object");
	this.__class.push("Kwik.Ui.Controls.Event");

	/** Type of event @type String */
	this.type = "";
	/** Dom event object which started this event @type HTMLEvent */
	this.srcEvent = null;
	/** Source element of the event @type HTMLElement */
	this.srcElement = null;
	/** Control object of the event @type Kwik.Ui.Controls.Control */
	this.control = null;
	/** Indication whether the alt key is pressed @type Boolean */
	this.altKey = false;
	/** Indication whether the control key is pressed @type Boolean */
	this.ctrlKey = false;
	/** Indication whether the shift key is pressed @type Boolean */
	this.shiftKey = false;
	/** Event creation date @type Date */
	this.created = new Date();
	/** Indication whether the event is canceled @type Boolean */
	this.isCanceled = false;
	
	/** Cancel the event bubble */
	this.stopPropagation = function Event_stopPropagation()
	{
		if(this.srcEvent)
		{
			if(typeof this.srcEvent.preventDefault != "undefined")
			{
				this.srcEvent.preventDefault();
			}
			if(typeof this.srcEvent.preventBubble != "undefined")
			{
				this.srcEvent.preventBubble();
			}
			if(typeof this.srcEvent.cancleBubble != "undefined")
			{
				this.srcEvent.cancleBubble = true;
			}
		}
		this.isCanceled = true;
	};

	/** Configure this event object @type Kwik.Ui.Controls.EventConfig */
	this.configure = function Column_configure(config)
	{
		config = this.Kwik$Object.configure(config);
		if(config.event)
		{
			this.srcEvent = config.event;
			this.srcElement = config.event.srcElement || config.event.target;
			this.altKey = config.event.altKey;
			this.ctrlKey = config.event.ctrlKey;
			this.shiftKey = config.event.shiftKey;
		}
		if(config.type){this.type = config.type;}
		if(config.control){this.control = config.control;}
		return config;
	};

	/** @ignore */
	this.main = function(config)
	{
		if(config){this.configure(config);}
	}; this.main(config);
};

/**
* Control event configuration object
* @constructor
* @created 11:24 AM 7/30/2009
* @modified 11:24 AM 7/30/2009
*/
Kwik.Ui.Controls.EventConfig= function()
{
	this.event = null;
	this.control = null;
	this.type = "";
};

/**
* Mouse event object
* @extends Kwik.Ui.Controls.Event
* @requires Kwik.Ui.Controls.Event
* @constructor
* @created 11:24 AM 7/30/2009
* @modified 11:24 AM 7/30/2009
*/
Kwik.Ui.Controls.KeyEvent = function(config)
{
	/** @ignore */
	Kwik.Utility.extend(this, "Kwik.Ui.Controls.Event");
	this.__class.push("Kwik.Ui.Controls.KeyEvent");

	/** Keycode of the key pressed @type Number */
	this.keyCode = -1;

	/** Configure this mouse event object @type Object */
	this.configure = function MouseEvent_configure(config)
	{
		config = this.Kwik$Ui$Controls$Event.configure(config)
		if(this.srcEvent)
		{
			this.keyCode = this.srcEvent.keyCode;
		}
		return config;
	};

	/** @ignore */
	this.main = function(config)
	{
		if(config){this.configure(config);}
	}; this.main(config);
};

/**
* Mouse event object
* @extends Kwik.Ui.Controls.Event
* @requires Kwik.Ui.Controls.Event
* @requires Kwik.Ui.Utility
* @constructor
* @created 11:24 AM 7/30/2009
* @modified 11:24 AM 7/30/2009
*/
Kwik.Ui.Controls.MouseEvent = function(config)
{
	/** @ignore */
	Kwik.Utility.extend(this, "Kwik.Ui.Controls.Event");
	this.__class.push("Kwik.Ui.Controls.MouseEvent");

	/** Screen x-coordinate of the mouse pointer @type Number */
	this.x = 0;
	/** Screen y-coordinate of the mouse pointer @type Number */
	this.y = 0;
	/** Element x-coordinate of the mouse pointer @type Number */
	this.offsetX = 0;
	/** Element y-coordinate of the mouse pointer @type Number */
	this.offsetY = 0;
	/** Button that started th event, either left=0, center=1 or right=2 @type Number */
	this.button = -1;
	/** Wheel scroll indication either 120 (up) or -120 (down) @type Number */
	this.wheelDelta = 0;


	/** Configure this mouse event object @type Object */
	this.configure = function MouseEvent_configure(config)
	{
		config = this.Kwik$Ui$Controls$Event.configure(config)
		if(this.srcEvent)
		{
			this.x = this.srcEvent.clientX;
			this.y = this.srcEvent.clientY;

			this.button = this.srcEvent.button;
			if(Kwik.BrowserInfo.internetExplorer)
			{
				this.button = this.button == 1? 0: this.button==4? 1: this.button;
			}

			this.wheelDelta = this.srcEvent.detail*-1 || this.srcEvent.wheelDelta;
			var delta = this.srcEvent.detail*-1 || this.srcEvent.wheelDelta;
			this.wheelDelta = this.wheelDelta < 0? -120: 120;

			this.offsetX = typeof this.srcEvent.layerX == "undefined"? this.srcEvent.offsetX: this.srcEvent.layerX;
			this.offsetY = typeof this.srcEvent.layerY == "undefined"? this.srcEvent.offsetY: this.srcEvent.layerY;
			this.offsetX = this.offsetX < 0? 0: this.offsetX;
			this.offsetY = this.offsetY < 0? 0: this.offsetY;
		}
		return config;
	};

	/** Get percentage of the mouseposition on the element from left to right @type Number */
	this.getPercentageX = function MouseEvent_getPercentageX()
	{
		if(!this._rect){this._rect = this.control.getRectangle();}
		return Math.round(this.offsetX * 100 / this._rect.w);
	};

	/** Get percentage of the mouseposition on the element from top to bottom @type Number */
	this.getPercentageY = function MouseEvent_getPercentageY()
	{
		if(!this._rect){this._rect = this.control.getRectangle();}
		return Math.round(this.offsetY / (this._rect.h / 100));
	};

	/** @ignore */
	this.main = function(config)
	{
		if(config){this.configure(config);}
	}; this.main(config);
};

/**
* User event enumeration
* @created 8:57 PM 5/6/2009
* @modified 8:58 PM 5/6/2009
* @singleton
*/
Kwik.Ui.Controls.UserEvents =
{
	onblur: "onblur",
	oncontextmenu: "oncontextmenu",
	onfocus: "focus",
	onresize: "onresize",
	onscroll: "onscroll",
	onselectstart: "onselectstart"
};

/**
* Key event enumeration
* @created 8:57 PM 5/6/2009
* @modified 8:58 PM 5/6/2009
* @singleton
*/
Kwik.Ui.Controls.KeyEvents =
{
	onkeydown: "onkeydown",
	onkeypress: "onkeypress",
	onkeyup: "onkeyup"
};

/**
* Mouse event enumeration
* @created 8:57 PM 5/6/2009
* @modified 8:58 PM 5/6/2009
* @singleton
*/
Kwik.Ui.Controls.MouseEvents =
{
	onclick: "onclick",
	ondblclick: "ondblclick",
	onmousedown: "onmousedown",
	onmousemove: "onmousemove",
	onmouseout: "onmouseout",
	onmouseover: "onmouseover",
	onmouseup: "onmouseup",
	onmousewheel: "onmousewheel"
};
